<?php

namespace app\admin\controller\general;

use app\admin\model\Admin;
use app\common\controller\Backend;
use fast\Random;
use think\Session;
use think\Validate;
use app\admin\model\Subject;
use app\admin\model\GradeGroup;
use app\admin\model\AuthGroupAccess;

/**
 * 个人配置
 *
 * @icon fa fa-user
 */
class Profile extends Backend
{
    protected $noNeedRight = ['businesstime'];
    protected $searchFields = 'id,title';

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $this->model = model('AdminLog');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->where('admin_id', $this->auth->id)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        //科目列表
        $subject_list = Subject::select();
        //年级组
        $grade_group_list = GradeGroup::select();
        $group_info = AuthGroupAccess::where('uid', $this->auth->id)->find();
        $this->view->assign("subject_list", $subject_list);
        $this->view->assign("group_info", $group_info);
        $this->view->assign("grade_group_list", $grade_group_list);
        return $this->view->fetch();
    }

    /**
     * 更新个人信息
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
//            var_dump($params);die;
            if ($params) {
                $params['f_major'] = isset($params['f_major']) ? implode(',',$params['f_major']) : '';
                // $params['f_major'] = implode(',',$params['f_major']);
                $group_info = AuthGroupAccess::where('uid', $this->auth->id)->find();
                if (isset($params['password']) && !empty($params['password'])) {
                    $check_pwd = check_pwd($params['password']);
                    if($check_pwd['code'] != 1)$this->error($check_pwd['msg']);
                    $params['salt'] = Random::alnum();
                    $params['password'] = md5(md5($params['password']) . $params['salt']);
                }
                if($group_info['group_id'] != 12){//普通教师
                    $validate = $this->validate($params, 'Admin.edit');
                }else{
                    if(empty($params['f_major']) && empty($params['major']))$this->error('主科和科任必须选一种');
                    $validate = $this->validate($params, 'Admin.teacher_edit');
                }
                if ($validate !== true) $this->error($validate);
                $admin = Admin::get($this->auth->id);
                $admin->save($params);
                //因为个人资料面板读取的Session显示，修改自己资料后同时更新Session
                Session::set("admin", $admin->toArray());
                $this->success();
            }
            $this->error();
        }
        return;
    }

    public function businesstime()
    {
        if ($this->request->isPost()) {
            $business_time = $this->request->post("business_time");
            $admin = Admin::get($this->auth->id);
            $admin->save(['business_time'=>$business_time]);
            $this->success(__('设置成功'), 'index/index', ['url' => 'index/index', 'id' => $this->auth->id, 'username' => $this->auth->username, 'avatar' => $this->auth->avatar]);
        }
        $this->view->assign("row", ['name'=>'']);
        return $this->view->fetch();
    }
}
